package org.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.checkerframework.checker.units.qual.C;
import org.example.entity.Contents;

import java.util.List;

@Mapper
public interface ContentsMapper extends BaseMapper<Contents> {


    // 根据用户ID查询内容生成记录
    @Select("SELECT * FROM contents WHERE user_id = #{userId} ORDER BY created_time DESC")
    List<Contents> getContentsByUserId(Long userId);
    
    // 根据用户ID查询内容生成记录（分页）
    @Select("SELECT * FROM contents WHERE user_id = #{userId} ORDER BY created_time DESC LIMIT #{offset}, #{size}")
    List<Contents> getContentsByUserIdWithPage(Long userId, Integer offset, Integer size);
    
    // 根据用户ID统计内容生成情况
    @Select("SELECT " +
            "COUNT(*) as totalGenerated, " +
            "SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as successGenerated, " +
            "SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as failedGenerated " +
            "FROM contents WHERE user_id = #{userId}")
    Contents getContentStats(Long userId);
    
    // 根据用户ID统计总消耗积分
    @Select("SELECT COALESCE(SUM(points_used), 0) FROM contents WHERE user_id = #{userId}")
    Integer getTotalPointsUsedByUserId(Long userId);
    
    // 调试：查询所有内容记录
    @Select("SELECT * FROM contents ORDER BY created_time DESC")
    List<Contents> getAllContents();
    
    // 调试：根据用户ID查询内容记录（使用字符串比较）
    @Select("SELECT * FROM contents WHERE CAST(user_id AS CHAR) = #{userId} ORDER BY created_time DESC")
    List<Contents> getContentsByUserIdAsString(String userId);
} 